package com.lagou.edu.service.impl;

import com.lagou.edu.client.AuthCodeApiService;
import com.lagou.edu.entity.AuthCode;
import com.lagou.edu.service.AuthCodeService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.Service;

import java.util.Date;
import java.util.Optional;

/**
 * @Author: yeqingyun
 * @Date: 2020/11/27 15:53
 */
@Service
@RequiredArgsConstructor
public class AuthCodeApiServiceImpl implements AuthCodeApiService {

    private final AuthCodeService authCodeService;

    @Override
    public Integer validate(String email, String code) {
        Optional<AuthCode> authCode = authCodeService.getByEmail(email);
        if (!authCode.isPresent()) {
            return 2;
        }
        if (!code.equals(authCode.get().getCode()) || authCode.get().getExpireTime().before(new Date())) {
            return 2;
        }

        return 0;
    }
}
